<?php

class FilialController extends Zend_Controller_Action
{
    public function init()
    {

    }

    public function indexAction(){
            $this->redirect('/filial/listar');
    }

    public function criarAction(){
        $this->_helper->viewRenderer->setNoRender();

        $post = $this->getAllParams();
       
        if (!is_null($this->getParam('msg')))
            $this->view->msg = Messages::getMessage($this->getParam('msg'));

        if (!empty($post['nome'])){
             
                $tFilial = new DbTable_Filial();
                $filial = $tFilial->createRow($this->getAllParams());
                $filial->nome = $post['nome'];
                $filial->cidade = $post['cidade'];
                $filial->save();
                $this->redirect('/filial/listar/msg/6');
        }
    }
    
    
    public function listarAction(){
        if (!is_null($this->getParam('msg')))
            $this->view->msg = Messages::getMessage($this->getParam('msg'));
        
        $tFilial = new DbTable_Filial();
        
        $this->view->filiais = $tFilial->fetchAll();
    }
    
    
    public function editarAction(){
        $this->_helper->viewRenderer->setNoRender();
        
        $post = $this->getAllParams();
        
        $tFilial = new DbTable_Filial();
        $filial = $tFilial->find($this->getParam('id'))->current();
    
        if (!empty($post['nome'])){
            $filial->nome = $post['nome'];
            $filial->cidade = $post['cidade'];
            $filial->status_filial = Status::DESBLOQUEADO;
            $filial->save();
            $this->redirect('/filial/listar/msg/6');
        }
    }
    
    
    public function ajaxBloquearAction(){
        $this->_helper->viewRenderer->setNoRender();
        
         $tFilial = new DbTable_Filial();
     
         $filial = $tFilial->find($this->getParam('id'))->current();
         
         if ((int) $filial->status_filial === Status::DESBLOQUEADO)
            $filial->status_filial = 1;
         else
             $filial->status_filial = 0;
         
        $filial->save();
        $this->redirect('/filial/listar/msg/6');
    }
   	
}

